
package com.wallj.videolibrary.player.renderer;

import android.content.Context;
import com.wallj.videolibrary.filters.GLPixelationFilter;
import com.wallj.videolibrary.utils.OpenGLUtils;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

/**
 * created by jw200 at 2019/4/8 11:22
 **/
public class PixelRenderer extends OesRenderer {

    GLPixelationFilter mPixelationFilter;

    public PixelRenderer(Context context) {
        super(context);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        super.onSurfaceChanged(gl, width, height);
        if (mPixelationFilter != null) {
            mPixelationFilter.onDisplaySizeChanged(width, height);
        }
    }

    @Override
    protected int onDrawFrameBuffer(int textureId, FloatBuffer vertexBuffer,
            FloatBuffer textureBuffer) {
        int bufferId = super.onDrawFrameBuffer(textureId, vertexBuffer, textureBuffer);
        if (mPixelationFilter != null) {
            bufferId = mPixelationFilter.drawFrameBuffer(
                    bufferId == OpenGLUtils.GL_NOT_TEXTURE ? textureId : bufferId, vertexBuffer,
                    textureBuffer);
        }
        return bufferId;
    }

    @Override
    protected void onChildFilterSizeChanged() {
        super.onChildFilterSizeChanged();
        if (mPixelationFilter == null) {
            mPixelationFilter = new GLPixelationFilter(mContext);
        }
        mPixelationFilter.initFrameBuffer(mIncommingWidth, mIncommingHeight);
        mPixelationFilter.onInputSizeChanged(mIncommingWidth, mIncommingHeight);
        mPixelationFilter.onDisplaySizeChanged(mSurfaceWidth, mSurfaceHeight);
    }

    @Override
    public void release() {
        super.release();
        if (mPixelationFilter != null) {
            mPixelationFilter.release();
            mPixelationFilter = null;
        }
    }
}
